package com.cc8w.admin.controller;

import javax.servlet.http.HttpServletRequest;

import com.cc8w.admin.entity.AdminUserPojo;
import com.cc8w.admin.service.UserService;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/admin/user")
public class UserController extends AdminBaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getlist")
    public String testReq(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "1") int size) throws JsonProcessingException {//查询全部数据
        //插件分页
        PageHelper.startPage(page,size); //这句话就可以使mybaits分页插件生效,pageInfo类里面有分页详细信息
        List<AdminUserPojo> adminUser = (ArrayList) userService.getUserAll();
        System.out.println(adminUser);


        //控制台打印json--start
        ObjectMapper mapper = new ObjectMapper();
        AdminUserPojo tt = new AdminUserPojo();
        tt.setUsername("222");
        tt.setCname(null);
        tt.setEmail(null);
        String str = mapper.writeValueAsString(tt);
        System.out.println(str);
        adminUser.add(tt);
        //控制台打印json--end

        this.model.addAttribute("admin_user", adminUser);
        return "admin/user/getlist";
    }

    @RequestMapping("/getlist2")
    //带默认值
    public String getList2(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "1") int size) throws JsonProcessingException {//分页查询数据

        //自定义分页
        List<AdminUserPojo> adminUser = (ArrayList) userService.getUserList(page, size);

        System.out.println(adminUser);
        ObjectMapper mapper = new ObjectMapper();

        AdminUserPojo tt = new AdminUserPojo();
        tt.setUsername("222");
        tt.setCname(null);
        tt.setEmail(null);
        String str = mapper.writeValueAsString(tt);
        System.out.println(str);


        this.model.addAttribute("admin_user", adminUser);
        return "admin/user/getlist";
    }

    @RequestMapping("/getpagelist")
    public String getPageList() {
        HashMap hm = new HashMap();
        hm.put("page", 0);
        hm.put("size", 2);
        hm.put("username", "fps2");
        List<AdminUserPojo> adminUser = (ArrayList<AdminUserPojo>) userService.getUserPageList(hm);
        int count = (int) userService.getUserPageCount(hm);
        System.out.println(adminUser);
        this.model.addAttribute("admin_user", adminUser);
        return "admin/user/getlist";
    }





}
